package cn.magictodo.weblog.common.domain.mapper;

import cn.magictodo.weblog.common.domain.dos.IpDo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDate;
import java.util.List;

/**
 * ipMapper
 *
 * @author liu
 * @since 2024-03-25 16:06
 */
@Mapper
public interface IpMapper extends BaseMapper<IpDo> {

    /**
     * 根据ip地址查询当日数据
     *
     * @param ip date
     * @return IpDo
     */
    default IpDo selectByIpWithDate(String ip,LocalDate date) {
        QueryWrapper<IpDo> ipQueryWrapper = new QueryWrapper<IpDo>().eq("ip", ip)
                .eq("DATE(create_time)",date);
        return selectOne(ipQueryWrapper);
    }

    /**
     * 根据日期查询独立IP（IP）
     *
     * @param date 日期
     * @return Long
     */
    default Long selectIPNumberByDate(LocalDate date){
        QueryWrapper<IpDo> ipQueryWrapper = new QueryWrapper<IpDo>()
                .eq("DATE(create_time)",date);
        return selectCount(ipQueryWrapper);
    }

//    根据日期查询访问列表
    default List<IpDo> selectIPByDate(LocalDate date){
        QueryWrapper<IpDo> ipQueryWrapper = new QueryWrapper<IpDo>()
                .select("id")
                .eq("DATE(create_time)",date);
        return selectList(ipQueryWrapper);
    }
}
